function set_wx_share(title,groupTitle,desc,img,link)
{
    wx.onMenuShareTimeline({
        title: groupTitle, // 分享标题
        link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: img, // 分享图标
        success: function () {
            // 用户确认分享后执行的回调函数

        },
        cancel: function () {
            // 用户取消分享后执行的回调函数
        }
    });

    //获取“分享给朋友”按钮点击状态及自定义分享内容接口
    wx.onMenuShareAppMessage({
        title: title, // 分享标题
        desc: desc, // 分享描述
        link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: img, // 分享图标
        success: function () {
            // 用户确认分享后执行的回调函数
            //alert('ok');
        },
        cancel: function () {
            // 用户取消分享后执行的回调函数
            //alert('faile');
        }
    });
}
//显示ppt
function show_ppt(id)
{
    $('#'+id).removeClass('hide');
    var s= slidr.create(id).start().auto();
    var length = $('#'+id + "> img").length;
    var arr = new Array();
        for(var i=0;i<length;i++)
        {
            arr.push("i"+i);
        }
        if(arr.length > 1)
        {
            arr.push("i0");
        }
        s.add('h', arr);
}


//提示
var notice = {
    callback:function(){},
    //显示提示
    show:function(msg){
        $('.notice-framework').show();
        $('.notice-framework .msg p').text(msg);
    },
    //关闭提示
    cancel:function(){
        $('.notice-framework').fadeOut();
    },
    //确定
    sure: function () {
        this.cancel();
        this.callback();
    },
    alert:function(msg){
        $('#cancelbtn').hide();
        this.callback = function(){};
        this.show(msg);
    },
    confirm:function(msg,callback){
        this.show(msg);
        $('#cancelbtn').show();
        this.callback = callback;
    }
};


var register = {
    time:0, //验证码倒计时
    canGetCode:true,
    canSubmit:true,
    //显示规则
    showRule:function(){
        $('.rule-detail').removeClass('hide');
        $('.rule-main').addClass('hide');
    },
    //关闭规则
    closeRule: function () {
        $('.rule-main').removeClass('hide');
        $('.rule-detail').addClass('hide');
    },
    //更换图片
    changeImg:function() {
        $('#headFile').click();
    },
    //显示预览图
    showImg: function (imgInfo) {
        $('.upload-img').css('background-image','url('+imgInfo.data+')');
    },
    //获得手机验证码
    getMobileCode:function()
    {
        var mobile     = this.getFrom('mobile');
        var country    = $('#country').val();
        if(mobile.length < 5)
        {
            notice.alert('请检查手机号');
            return false;
        }
        if(this.time > 0)
        {
            notice.alert('发送中，请稍等···');
            return false;
        }
        $('#codebtn').text('请稍等···');

        this.ajaxGetCode(mobile,country);
    },
    getFrom:function(id){
        return $.trim($('#'+id).val());
    },

    //检查用户是否注册
    check_user_exists:function(mobile,callback){
        var url     = appRoot + 'web_check_user_exists';
        var data    = {mobile:mobile};
        var token   = $('meta[name="csrf-token"]').attr('content');
        $.ajax({
            url:url,
            type:'post',
            data:data,
            headers: {'X-CSRF-TOKEN': token},
            success:function(res){
                callback(res);
            }
        })
    },


    //发送验证码
    ajaxGetCode:function (mobile,country) {
        var token= $('meta[name="csrf-token"]').attr('content');
        var data = {
            mobile:mobile,
            country:country
        };
        var    url  = appRoot + "mobile/getCode";

        $.ajax({
            type: "POST",
            url: url,
            data: data,
            headers: {'X-CSRF-TOKEN': token},
            success: function (msg) {
                if(msg.code == 200){
                    //获取成功，开始计时
                    register.time = 120;
                    register.setTime();
                }else{
                    //获取失败  给提示 恢复按钮可点
                    alert(msg.message);
                }
            }
        });
    },

    setTime:function(){
        var time = register.time;
            if(time > 1)
            {
                var text = "发送中(" + time + ")";
                $("#codebtn").text(text);
                register.time--;
                setTimeout(register.setTime,1000);
            }else{
                $("#codebtn").text("重新发送");
                register.time = 0;
            }
    },

    //检查验证码
    ajaxCheckMobCode:function (mobcode, mobile) {
        var url     = appRoot + "mobile/checkCode";
        var token   = $('meta[name="csrf-token"]').attr('content');
        var data    = {code: mobcode, mobile: mobile};
        var result  = false;
        $.ajax({
            type: "POST",
            async: false,
            url: url,
            data: data,
            headers: {
                'X-CSRF-TOKEN':token
            },
            success: function (msg) {
                var valid   = msg.data.valid;
                result      = valid == 1 ? true : false;
            }
        });
        return result;
    },

    //提交订单
    submitOrder:function()
    {

        var mobile  = this.getFrom('mobile');
        var name    = this.getFrom('name');
        var code    = this.getFrom('code');
        var agree   = $('input[name="agree"]:checked').val();

        if(name.length == 0){
            notice.alert('请填写姓名!');
        }else if(!(/^[0-9]{6,20}$/).test(mobile)){
            notice.alert('请检查手机号');
        } else if(!this.ajaxCheckMobCode(code,mobile)){
            notice.alert('手机号验证失败');
        }else if(code.length == 0)
        {
            notice.alert('请检查验证码！')
        }else if(agree == 0){
            notice.alert('您沒有同意用户协议！');
        }else{
            notice.confirm('确定提交吗?',this.submit)
        }
    },
    submit:function()
    {
        $('form').submit();
    }
};

function stop_event(e)
{
     if(e && e.stopPropagation) { //非IE 
          e.stopPropagation(); 
    } else { //IE 
          window.event.cancelBubble = true; 
    } 
}


var order = {
    canPay:true,//是否可支付
    oldMoney:0,//以往的筹资金额
    anonymous:0,
    payConfig:{},//支付配置
    //切换导航
    trrigleNavs:function(id){
        $('.content  > * ').addClass('hide');
        $('#'+id).removeClass('hide');

        $('.nav  > *').removeClass('selected');
        $('.nav-'+id).addClass('selected');
    },
    //显示支付界面
    showPayPage:function(){
        $('.pay-page').removeClass('hide');
        lockScroll();
    },
    //关闭支付界面
    closePayPage:function(){
        $('.pay-page').addClass('hide');
        relieveScroll();
    },


    cancel_radio:function(){

        $('.radio').prop('checked',false);
        console.log($('.radio'));

    },

    cancel_input:function()
    {
        $('.tel').val('');
    },
    moneyNum:0,
    change_money:function(obj){
        if(obj !== "input")
        {
            $('.moneyNum').removeClass('selected');
            $(obj).addClass('selected');
            money   = $(obj).data('money');
            order.moneyNum = $('#otherMoney').val();
            $('#otherMoney').val('');
            $('#payNum').text("合计:"+money + "元");
        }else{
            $('.moneyNum').removeClass('selected');
            $('#otherMoney').val(order.moneyNum);
        }
    },
    show_money:function()
    {
        var money = $('#otherMoney').val();
        $('#payNum').text("合计:"+money + "元");
        order.moneyNum  = money;
    },
    //获得支付的数据
    getPayData:function(){

        var name        = $('#cusName').val();
        var mobile      = $('#cusMobile').val();
        var orderId     = url_get('orderId');
        var isAnonymous = 0;


        var currentUrl  = location.href;
        var msg         = $('#comment').val();
        var money       = $('#otherMoney').val();

            if(money <= 0.01)
            {
                var mobj    = $('.selected');
                if(mobj.length == 1)
                {
                    money   = mobj.data('money');
                }
            }



            // if(name.length == 0)
            // {
            //     notice.alert('填写你的姓名哦');
            //     return;
            // }

            // if(isPoneAvailable(mobile) == false)
            // {
            //     notice.alert('请填写正确的手机号哦');
            //     return ;
            // }


            if(money < 0.01 || money > 50000)
            {
                notice.alert('支持金额大于0.01小于50000哦');
                return false;
            }

            if(this.canPay == false)
            {
                return false;
            }

            this.canPay = false;

        var data        =
            {
                orderId:orderId,
                money:money,
                isAnonymous:isAnonymous,
                url:currentUrl,
                msg:msg,
                name:name,
                mobile:mobile
            };


        var url = appRoot + "www/zhongchou666/enter?fun=get_pay_data";
        var token   = $('meta[name="csrf-token"]').attr('content');
        $.ajax({
            type:'post',
            async: false,
            url: url,
            data: data,
            headers: {
                'X-CSRF-TOKEN':token
            },
            success: function (msg) {
                if(msg.code != 200)
                {
                    notice.alert(msg.message);
                    order.canPay = true;
                    return false;
                }
                var payConfig = msg.data.jsConfig;
                order.payConfig = payConfig;
                order.wx_pay();
            }
        });
    },
    //调起位置支付
    wx_pay:function()
    {
        WeixinJSBridge.invoke('getBrandWCPayRequest', this.payConfig, function(res)
        {

            //支付成功，不能再次支付了
            if(res.err_msg == "get_brand_wcpay_request:ok" )
            {
                notice.show('支付成功，感谢您的支持！');
                setTimeout(function(){
                    //history.back();
                    location.replace('/www/zhongchou666/order?orderId='+url_get('orderId'));
                },1000);
            }else{ //支付失败 可以再次支付

                order.canPay = true;
            }
        })
    },
    fresh_time:function(time){
        var time1   = time;
        var day     = parseInt(time1/(24*60*60));
            time1   = time1%(24*60*60);
        var hour    = parseInt(time1/(60*60));
            time1   = time1%(60*60);
        var min     = parseInt(time1/60);
        var sen     = time1%60;
        var timetext = "剩";
        if(day > 0){
            timetext += day + "天";
        }
        timetext += this.formatnum(hour) + ":" + this.formatnum(min) + ":" + this.formatnum(sen) + "";
        $('#order-time').text(timetext);
        if(time <= 0){
            $('#order-time').text("众筹已经超时");
            return ;
        }
        setTimeout(function(){
            order.fresh_time(time-1);
        },1000)
    },
    formatnum:function(num){
        if(num < 10){
            return "0"+num;
        }else{
            return num;
        }
    },
    top:function()
    {
        $('body').scrollTop(0);
    },
    orderPage:0,//订单页数
    orders: new Array(),
    //获得订单列表
    get_orders:function(){
        this.orderPage++;
        var data = {
            managerId:0,
            isSuccess:1,
            page:order.orderPage
        };
        var url = appRoot + "www/zhongchou666/enter?fun=page_orders";
        var token   = $('meta[name="csrf-token"]').attr('content');
        $.ajax({
            type:'post',
            async: false,
            url: url,
            data: data,
            headers: {
                'X-CSRF-TOKEN':token
            },
            success: function (msg)
            {
                var data = msg.data.orders;
                order.orders = order.orders.concat(data.data);
                fresh_orders(data.data,data.next_page_url,order.orderPage);
            }
        });
    },
    zankai:function(){
        $('.other-content').removeClass('hide');
        $('.zankai').addClass('hide');
        $('.zankai.close').removeClass('hide');

    },
    close:function(){
        $('.other-content').addClass('hide');
        $('.zankai').addClass('hide');
        $('.zankai.open').removeClass('hide');
    },
    //显示评论输入框
    show_comment_input:function(payId)
    {
        $('.add-comment-box').removeClass('hide').data('id',payId);
        $('.add-comment-box textarea').focus();
    },
    close_comment_input:function()
    {
        $('.add-comment-box').addClass('hide');
    },
    add_comment:function()
    {
        var comment = $('#commentinput').val();
        var id      = $('.add-comment-box').data('id');
        var data    = {comment:comment,payId:id};
        var url     = appRoot + "www/zhongchou666/add_order_comment";
        var token   = $('meta[name="csrf-token"]').attr('content');
            if(comment.length == 0)
            {
                notice.alert('评论内容不能为空');
                return;
            }

            if(comment.length > 50)
            {
                notice.alert('评论太长，请少于50字');
                return false;
            }

            $.ajax({
                type:'post',
                async: false,
                url: url,
                data: data,
                headers: {
                    'X-CSRF-TOKEN':token
                },
                success: function (res)
                {
                    location.reload()
                    // var comment = res.data.comment;
                    // $('#pay-'+id).append('<li>'+ comment.wx_name + ":" + comment.content+'</li>');
                    // $('#commentinput').val('');
                    // order.close_comment_input();
                }
            });
    },
    yanshi:function(orderId){

        notice.confirm("确定延长时间吗",function()
        {
            var url =appRoot + "www/zhongchou666/yanshi?orderId=" + orderId;
            $.ajax({
                url:url,
                type:'get',
                success:function(res)
                {
                    location.reload();
                }
            })
        })
    }
};


function isPoneAvailable(mobile) {
    var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(mobile)) {
        return false;
    } else {
        return true;
    }
}

function bindManager(activityId, openid) {
    notice.confirm('绑定为发起人吗', () => {
        var url = appRoot + "www/zhongchou666/bind_manager";
        var token   = $('meta[name="csrf-token"]').attr('content');
        $.ajax({
            type:'post',
            async: false,
            url: url,
            data: {activityId: activityId, openid: openid},
            headers: {
                'X-CSRF-TOKEN':token
            },
            success: function (msg) {
                console.log(msg)
                if(msg.code == 200) {
                    notice.alert('绑定成功');
                } else {
                    notice.alert('绑定失败');
                }
            }
        })
    })
}